import axios from "axios";
import qs from "qs";
import router from "../router";
const BASE_URL = process.env.VUE_APP_REQUEST_URL;

const service = axios.create({
    baseURL: BASE_URL,
    withCredentials: true,
    headers: {
        'token': localStorage.getItem("token")
    },
    devServer: {

        port: 8080,
        assetsSubDirectory: "static",
        assetsPublicPath: "/",
        proxyTable: {
            "/": {
                changeOrigin: true,
                pathRewrite: {
                    "^/": "",
                },
            },
        },
    },
});

service.interceptors.request.use(
    (config) => {
        if (config.method === "post") {
            config.data = qs.stringify(config.data);
        }
        if (config.method === "get") {
            config.url += "?" + qs.stringify(config.data);
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    (response) => {
        if (response.data.code == 203) {
            router.push("/login");
        } else {
            return response;
        }
    },
    (error) => {
        return Promise.reject(error);
    }
);

export default service;
